package com.example.myapplication.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.base.BaseRlvAdapter;
import com.example.myapplication.bean.SquaseBean;

import java.util.ArrayList;
import java.util.List;

public class SquareAdapter extends RecyclerView.Adapter<SquareAdapter.ViewHolder> {

    public List<SquaseBean.DataBean.DatasBean> list = new ArrayList<>();
    private Context context;
    private OnItemClickListener onItemClickListener;
    public interface OnItemClickListener{
        void OnItemClick(int position,List<SquaseBean.DataBean.DatasBean> list);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public SquareAdapter(Context context) {

        this.context = context;
    }

    public void setList(List<SquaseBean.DataBean.DatasBean> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_square, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        SquaseBean.DataBean.DatasBean datasBean = list.get(position);
        holder.niceDate.setText(datasBean.getNiceDate());
        holder.chapterName.setText(datasBean.getChapterName());
        holder.superChapterName.setText(datasBean.getSuperChapterName());
        holder.title.setText(datasBean.getTitle());
        holder.shareUser.setText(datasBean.getShareUser());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener!=null){
                    onItemClickListener.OnItemClick(position,list);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView shareUser;
        public TextView niceDate;
        public TextView title;
        public TextView superChapterName;
        public TextView chapterName;
        public ImageView iv;

        public ViewHolder(View rootView) {
            super(rootView);
            this.shareUser = (TextView) rootView.findViewById(R.id.shareUser);
            this.niceDate = (TextView) rootView.findViewById(R.id.niceDate);
            this.title = (TextView) rootView.findViewById(R.id.title);
            this.superChapterName = (TextView) rootView.findViewById(R.id.superChapterName);
            this.chapterName = (TextView) rootView.findViewById(R.id.chapterName);
            this.iv = (ImageView) rootView.findViewById(R.id.iv);
        }
    }
}
